//
//  YLNewPasswordViewModel.m
//  YLFinanceService
//
//  Created by she1323 on 2017/8/21.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLNewPasswordViewModel.h"
#import "WHProgressHUD.h"
#import "NSString+Regex.h"
#import "WHNetworkTool.h"
#import "NSString+Hash.h"
#import "YLAccountTool.h"
@implementation YLNewPasswordViewModel

- (instancetype)init
{
    if (self = [super init]) {
        [self setUp];
    }
    return self;
}

- (RACCommand *)setPasswordCommand
{
    if (!_setPasswordCommand) {
        __weak typeof(self) weakSelf = self;
    
        _setPasswordCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                
                // 判断密码
                {
                    NSString *message = [NSString isOrNoPasswordStyle:weakSelf.password];
                    if (message.length != 0) {
                        [WHProgressHUD showError:message];
                        [subscriber sendCompleted];
                        return nil;
                    }
                }
                
                if (![weakSelf.passwordSure isEqualToString:weakSelf.password]) {
                    [WHProgressHUD showError:@"两次密码不一致"];
                    [subscriber sendCompleted];
                    return nil;
                }
                
                NSString *url = [NSString stringWithFormat:@"%@Account/ModifyPassWordByCode", YLURL];
                
                NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:4];
                params[@"NewPassWord"] = [weakSelf.password md5String];
                params[@"NewPassWordAgain"] = [weakSelf.password md5String];
                params[@"VerifyCode"] = weakSelf.verifyCode;
                params[@"TelCode"] = weakSelf.telCode;
                
                [[WHNetworkTool shareInstance] sendPostWithUrl:url header:[YLAccountTool header] params:params success:^(id responseObject) {
                    if ([responseObject[@"httpCode"] integerValue] == 200) {
                        [subscriber sendNext:nil];
                    } else {
                        [WHProgressHUD showError:responseObject[@"message"]];
                    }
                    [subscriber sendCompleted];
                    
                } fail:^(NSError *error) {
                    [subscriber sendCompleted];
                }];
                
                
                return nil;
            }];
            
        }];
    }
    return _setPasswordCommand;
}

- (void)setUp
{
    _passwordSetEnableSiganl = [RACSignal combineLatest:@[RACObserve(self, password), RACObserve(self, passwordSure)] reduce:^id _Nullable(NSString *password, NSString *passwordSure){
        return @(password.length && passwordSure.length);
    }];
    
  
}


@end
